package fun.sweeper.unity.controller;

import fun.sweeper.base.controller.BaseController;
import fun.sweeper.base.entity.AjaxResult;
import fun.sweeper.unity.dto.OrderPayDto;
import fun.sweeper.unity.entity.HhOrder;
import fun.sweeper.unity.service.HhOrderService;
import fun.sweeper.unity.utils.ParamsDictUtil;
import fun.sweeper.unity.utils.PrintPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 销售订单Controller
 * @author chongjin
 * @date 2020-11-18
 */
@Controller
public class HhOrderController extends BaseController {
    private String prefix = "hh/saleManage/order";

    @Autowired
    private HhOrderService hhOrderService;

    @GetMapping("/order/manage")
    public String manage() {
        return prefix + "/order_manage";
    }

    /**
     * 查询销售订单列表
     */
    @PostMapping("/order/manage/list")
    @ResponseBody
    public List<HhOrder> list(HhOrder hhOrder) {
//        hhOrder.setOrderType(ParamsDictUtil.ORDER_TYPE_SR);
        List<HhOrder> list = hhOrderService.selectHhOrderList(hhOrder);
        return list;
    }

    /**
     * 新增订单页面
     */
    @GetMapping("/order/manage/add")
    public String add(ModelMap mmap) {
        mmap.put("orderId", hhOrderService.createOrderId());
        mmap.put("selectLength", PrintPageUtil.pageSize);
        return prefix + "/order_add";
    }

    /**
     * 新增保存销售订单
     */
    @PostMapping("/order/manage/add")
    @ResponseBody
    public AjaxResult addSave(HhOrder hhOrder) {
        return toAjax(hhOrderService.insertHhOrder(hhOrder));
    }

    /**
     * 新开单
     */
    @PostMapping("/order/manage/create")
    @ResponseBody
    public AjaxResult createOrder(HhOrder hhOrder) {
        return AjaxResult.success(hhOrderService.createOrder(hhOrder));
    }

    /**
     * 新开单
     */
    @PostMapping("/order/manage/createCheck")
    @ResponseBody
    public AjaxResult createCheck(HhOrder hhOrder) {
        return AjaxResult.success(hhOrderService.createCheck(hhOrder));
    }

    /**
     * 获取修改订单页面
     */
    @GetMapping("/order/manage/edit/{orderId}")
    public String getEditHhOrder(@PathVariable("orderId") String orderId, ModelMap mmap) {
        HhOrder hhOrder = hhOrderService.getEditHhOrder(orderId);
        mmap.put("hhOrder", hhOrder);
        mmap.put("selectLength", PrintPageUtil.pageSize);
        String url = ParamsDictUtil.ORDER_TYPE_RETURN.equals(hhOrder.getType()) ? "/return_edit" : "/order_edit";
        return prefix + url;
    }

    /**
     * 修改销售订单
     */
    @PostMapping("/order/manage/edit")
    @ResponseBody
    public AjaxResult editSave(HhOrder hhOrder) {
        return toAjax(hhOrderService.updateHhOrder(hhOrder));
    }

    /**
     * 删除销售订单
     */
    @PostMapping("/order/manage/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(hhOrderService.deleteHhOrderByIds(ids));
    }

    /**
     * 作废销售订单
     */
    @PostMapping("/order/manage/delete")
    @ResponseBody
    public AjaxResult delete(String orderId) {
        return toAjax(hhOrderService.deleteHhOrderById(orderId));
    }

    /**
     * 查看销售订单
     */
    @GetMapping("/order/manage/view/{orderId}")
    public String orderView(@PathVariable("orderId") String orderId, ModelMap mmap) {
        HhOrder hhOrder = hhOrderService.selectHhOrderById(orderId);
        mmap.put("hhOrder", hhOrder);
        return prefix + "/order_view";
    }

    /**
     * 获取订单付款页面
     */
    @GetMapping("/order/manage/pay/{orderId}")
    public String getPayInfo(@PathVariable("orderId") String orderId, ModelMap mmap) {
        Map payInfo = hhOrderService.getPayInfo(orderId);
        mmap.put("payInfo", payInfo);
        return prefix + "/order_pay";
    }

    /**
     * 获取订单结算退货页面
     */
    @GetMapping("/order/manage/return/{orderId}")
    public String getReturnInfo(@PathVariable("orderId") String orderId, ModelMap mmap) {
        Map payInfo = hhOrderService.getPayInfo(orderId);
        mmap.put("payInfo", payInfo);
        return prefix + "/order_return";
    }

    /**
     * 获取退货单页面
     */
    @GetMapping("/order/manage/addReturn/{orderId}")
    public String addReturn(@PathVariable("orderId") String orderId, ModelMap mmap) {
        mmap.put("selectLength", PrintPageUtil.pageSize);
        mmap.put("hhOrder", hhOrderService.createReturnHhOrder(orderId));
        return prefix + "/return_edit";
    }

    /**
     * 销售单付款
     */
    @PostMapping("/order/manage/pay")
    @ResponseBody
    public AjaxResult orderPay(OrderPayDto orderPayDto) {
        return toAjax(hhOrderService.orderPay(orderPayDto));
    }

}
